package com.dcbeida.activity.salsesdedail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.applyreturn.ApplyReturnActivity;
import com.dcbeida.adapter.SalsesDetail2Adapter;
import com.dcbeida.adapter.SalsesDetailAdapter;
import com.dcbeida.adapter.SalsesDetailRYAdapter;
import com.dcbeida.bean.CardEnum;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.MyClientBean;
import com.dcbeida.bean.Sales;
import com.dcbeida.bean.SalsesDetail;
import com.dcbeida.bean.SignStatusEnum;
import com.dcbeida.utils.Constants;
import com.dcbeida.widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 销售详情
 * Created by swt on 2017/9/29.
 */

public class SalsesDetailActivity extends BaseActivity implements AdapterView.OnItemClickListener, SalsesDetailContract.View {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.myList)
    ListViewForScrollView myList;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.tv_bh)
    TextView tvBh;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_dh)
    TextView tvDh;
    @Bind(R.id.tv_sfz)
    TextView tvSfz;
    @Bind(R.id.tv_yy_name)
    TextView tvYyName;
    @Bind(R.id.tv_box_num)
    TextView tvBoxNum;
    @Bind(R.id.tv_money)
    TextView tvMoney;
    @Bind(R.id.tv_sqth)
    TextView tvSqth;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;

    @Bind(R.id.recyclerview2)
    RecyclerView recyclerview2;

    @Bind(R.id.ll_notfp)
    LinearLayout ll_notfp;

    @Bind(R.id.ll_notfp2)
    LinearLayout ll_notfp2;

    @Bind(R.id.tv_xsypxx)
    TextView tvXsypxx;
    @Bind(R.id.tv_error)
    TextView tv_error;

    @Bind(R.id.tv_sign_status)
    TextView tv_sign_status;

    @Bind(R.id.tv_doctor)
    TextView tv_doctor;


    private SalsesDetail2Adapter adapter;
    private SalsesDetailPresenter mPresent;
    private Sales.ListBean salesBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.salses_detail;
    }

    @Override
    public void setData() {
        super.setData();
        tvSqth.setVisibility(View.GONE);
        tvXsypxx.setVisibility(View.GONE);

        imgBack.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.xsxq);
        tvRight.setText(R.string.bj);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            salesBean = (Sales.ListBean) bundle.getSerializable("Sales.ListBean");
            tvBh.setText(mContext.getString(R.string.bh) + salesBean.getId() + "");
            tvTime.setText(salesBean.getCreatetime() + "");
            tvName.setText(salesBean.getTruename() + "");
            tvDh.setText(getString(R.string.lxfs) + salesBean.getPhonenum() + "");
            tvBoxNum.setText(salesBean.getQuantity() + "");
            String cardName = CardEnum.getCardName(salesBean.getCardtype());
            tvSfz.setText(cardName+":"  + salesBean.getIdcardnum() + "");
            tv_doctor.setText("医生:"+salesBean.getDoctorname());
            tvYyName.setText(salesBean.getHospitalname() + "");
            tvMoney.setText(getString(R.string.yuan) + salesBean.getAmount() + "");
            if(!TextUtils.isEmpty(salesBean.getInfostatusDesc())){
                tv_error.setVisibility(View.VISIBLE);
                tv_error.setText("信息缺失: "+salesBean.getInfostatusDesc());
            }



            //图片地址
            ArrayList<String> paths = new ArrayList<>();
            if (!TextUtils.isEmpty(salesBean.getInvoiceimgurl())) {
                ll_notfp.setVisibility(View.GONE);
                recyclerview.setVisibility(View.VISIBLE);
                if (salesBean.getInvoiceimgurl().contains(",")) {
                    String[] imgUrl = salesBean.getInvoiceimgurl().split(",");
                    for (int i = 0; i < imgUrl.length; i++) {
                        paths.add(imgUrl[i]);
                    }
                } else {
                    paths.add(salesBean.getInvoiceimgurl());
                }
            } else {
                ll_notfp.setVisibility(View.VISIBLE);
                recyclerview.setVisibility(View.GONE);
            }

            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerview.setLayoutManager(layoutManager);
            //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
            recyclerview.setHasFixedSize(true);
            SalsesDetailRYAdapter ryAdapter = new SalsesDetailRYAdapter(mContext);
            recyclerview.setAdapter(ryAdapter);
            ryAdapter.setData(paths);




            //图片地址
            ArrayList<String> paths2 = new ArrayList<>();
            if (!TextUtils.isEmpty(salesBean.getRecipepicsurl())) {
                ll_notfp2.setVisibility(View.GONE);
                recyclerview2.setVisibility(View.VISIBLE);
                if (salesBean.getRecipepicsurl().contains(",")) {
                    String[] imgUrl = salesBean.getRecipepicsurl().split(",");
                    for (int i = 0; i < imgUrl.length; i++) {
                        paths2.add(imgUrl[i]);
                    }
                } else {
                    paths2.add(salesBean.getRecipepicsurl());
                }
            } else {
                ll_notfp2.setVisibility(View.VISIBLE);
                recyclerview2.setVisibility(View.GONE);
            }

            LinearLayoutManager layoutManager2 = new LinearLayoutManager(mContext);
            layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerview2.setLayoutManager(layoutManager2);
            //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
            recyclerview2.setHasFixedSize(true);
            SalsesDetailRYAdapter ryAdapter2 = new SalsesDetailRYAdapter(mContext);
            recyclerview2.setAdapter(ryAdapter2);
            ryAdapter2.setData(paths2);



            scrollView.smoothScrollTo(0, 0);
            myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
            myList.setOnItemClickListener(this);
            adapter = new SalsesDetail2Adapter(this);
            myList.setAdapter(adapter);

            mPresent = new SalsesDetailPresenter(this, this);
            mPresent.getSalsesDetailInfo(MyApplication.user.getUid(), salesBean.getId(), "0");

            mPresent.queryPatient(Integer.parseInt(salesBean.getPatientid()));
        }


    }


    @OnClick({R.id.img_back, R.id.tv_right, R.id.tv_sqth})
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                bundle.putSerializable("Sales.ListBean", salesBean);
                showActivity(SalsesDetailUpdateActivity.class, bundle);

                break;
            case R.id.tv_sqth:

                bundle.putSerializable("Sales.ListBean", salesBean);
                showActivityForResult(ApplyReturnActivity.class, bundle, Constants.ONE);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void setPresenter(SalsesDetailContract.Present presenter) {

    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }

    @Override
    public void showSalsesDetailList(ArrayList<SalsesDetail.ListBean> list) {
        ArrayList<SalsesDetail.ListBean> tempList = new ArrayList<>();
        DrugListBean listBean = MyApplication.getInstance().getmDrugBean();
        if (listBean == null || listBean.getList() == null)
            return;

        for (Drug drug : listBean.getList()) {
            List<SalsesDetail.ListBean> drugList = new ArrayList<>();
            int count = 0;
            for (SalsesDetail.ListBean bean : list) {
                    if (drug.getDrugid() == bean.getDrugid()) {
                        count += Integer.parseInt(bean.getQuantity());
                        bean.setDrugName(drug.getDrugname());
                        drugList.add(bean);
                    }

            }
            if (drugList.size() > 0) {
                SalsesDetail.ListBean data = drugList.get(0);
                data.setFirst(true);
                data.setCount(count);
            }
            tempList.addAll(drugList);
        }

        if (tempList.size() > 0) {
            tvSqth.setVisibility(View.VISIBLE);
            //tvXsypxx.setVisibility(View.VISIBLE);
        } else {
            tvSqth.setVisibility(View.GONE);
            tvXsypxx.setVisibility(View.GONE);
        }
        adapter.setmArrayList(tempList);

    }

    @Override
    public void showList(ArrayList<MyClientBean> list) {

    }

    @Override
    public void showSignStatus(int signStatus) {
        tv_sign_status.setText("知情书签署情况:"+ SignStatusEnum.getCardName(signStatus));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Constants.ONE) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
